package com.loren.demo09.controller;

import com.github.benmanes.caffeine.cache.Cache;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class DemoController {

    private final Cache<String, String> fixedTimeCacheMachine;

    private final Cache<String, String> sessionCacheMachine;


    @GetMapping("/demo01")
    public String demo01() {
        this.fixedTimeCacheMachine.put("aaa", "hello fixed time caffeine");
        return "success";
    }

    @GetMapping("/demo02")
    public String demo02() {
        String caffeine = this.fixedTimeCacheMachine.getIfPresent("aaa");
        if(caffeine != null) {
            return caffeine;
        }
        return "cache has been removed";
    }


    @GetMapping("/demo03")
    public String demo03() {
        this.sessionCacheMachine.put("bbb", "hello session caffeine");
        return "success";
    }

    @GetMapping("/demo04")
    public String demo04() {
        String caffeine = this.sessionCacheMachine.getIfPresent("bbb");
        if(caffeine != null) {
            return caffeine;
        }
        return "cache has been removed";
    }
}
